<template>
  <div class="myContainer">
    <el-row class="myBorderBottom">
      <el-col :span="3" class="myTitleText myLineHeight_40px">设备台账记录</el-col>
      <el-col :span="21" class="myTextAlignRight">
        <!-- <el-button type="primary" class="myBgGreen" @click="onAdd">新增维保记录</el-button> -->
      </el-col>
    </el-row>
    <!-- 搜索部分 start -->
    <el-row :gutter="10" class="myPaddingTop_10px">
      <el-col :span="6">
        <template>
         <el-cascader
            placeholder="全部项目/小区"
            :options="regionOptions"
            v-model="regionOptSelected"
            ></el-cascader>
        </template>
      </el-col>
      <el-col :span="4">
        <template>
          <el-select
            v-model="devRoomSelected"
            clearable
            @visible-change="handleDevRoomChange"
            placeholder="设备房">
            <el-option
              v-for="item in devRoomList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-col>
      <el-col :span="4">
        <template>
          <el-select v-model="devTypeSelected"
            clearable
            placeholder="请选择设备类型">
            <el-option
              v-for="item in Object.keys(devTypeList)"
              :key="devTypeList[item]"
              :label="devTypeList[item]"
              :value="item">
            </el-option>
          </el-select>
        </template>
      </el-col>
      <el-col :span="4">
        <el-input v-model="kw" placeholder="输入关键词进行查询" clearable></el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" class="myBgGreen" @click="onSearch">查 询</el-button>
      </el-col>
    </el-row>
    <!-- 搜索部分 end -->
    <el-row class="mymarginTop_10px">
      <el-col :span="24">
        <template>
          <el-table
            border
            :stripe = true
            v-loading="loading"
            :data="list"
            style="width: 100%">
            <el-table-column
              label="序号"
              type="index"
              width="100">
            </el-table-column>
            <el-table-column
              label="设备名称"
              prop="dev_name">
            </el-table-column>
            <el-table-column
              label="设备编号"
              width="120"
              prop="dev_no">
            </el-table-column>
            <el-table-column
              label="设备类型">
              <template scope="scope">
                {{scope.row.dev_type | onDev_type}}
              </template>
            </el-table-column>
            <el-table-column
              label="所属小区"
              prop="area_name">
            </el-table-column>
            <el-table-column
              width="100"
              label="功能操作">
              <template scope="scope">
                <el-button class="look" type="text" size="small" @click="onDetailed(scope.row.dev_id)">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-col>
    </el-row>
    <!-- 分页控件 start -->
    <div class="block">                    
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page_num"
        :page-sizes="[10, 15]"
        :page-size="page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <!-- 分页控件 end -->
  </div>
</template>  

<style scoped>
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myHeadeTitle{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    padding-bottom: 10px;
    border-bottom: 1px solid #dbdbdb;
    margin-bottom:10px; 
  }
  .myHeadeTitle:before,.myHeadeTitle:after{
    content: " ";
    display: block;
    clear: both;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top: 2px;
  }
  .myLineHeight_35px{
    line-height: 35px;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myPaddingTop_10px{
    padding-top: 10px;
  }  
  .myLeftMsg{
    line-height: 40px;
    text-align: right;
    padding-right: 10px;
  }
  .myPaddingBottom_10px{
    padding-bottom: 10px;
  }
  .myVA_M{
    vertical-align: middle;
  }
  .myPadding_20px{
    padding: 20px;
  }    
  .myTitleText{
    font-weight: 500;
    font-size: 16px;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myBorderBottom{
    border-bottom:1px solid #dbdbdb;
  }
  .mymarginTop_10px{
    padding-top: 10px;
  }
</style>

<script>
import { extractAreaIds } from '@/utils/misc';
import { getRegion } from '@/utils/localstorage';
import { getDevRoomList } from '@/api/dev_room';
import { getDeviceList, devTypeList } from '@/api/device';

  export default {
     data(){
      return{
         // 搜索条件
        // 小区选择
        regionOptions: [],
        regionOptSelected: [],

        // 列表
        loading: false,
        list: [],
        page_num:1,
        page_size:10,
        total:null,

        // 设备房
        devRoomListLoading: false,
        devRoomList: [],
        devRoomSelected: null,

        // 设备类型
        devTypeList: devTypeList,
        devTypeSelected: null,
        // 关键字
        kw:"",

        housValue:"",
        housOptions:[],
        value1:"",
        value:""
      }
    },
    computed: {
      areaIds() {
        return extractAreaIds(this.regionOptSelected, this.regionOptions);
      }
    },
    filters:{
      onDev_type(val){
        return devTypeList[val];
      }
    },
    created(){
      document.title="设备台账记录";
      this.regionOptions = getRegion();
      this.regionOptSelected.push(this.regionOptions[0].value,this.regionOptions[0].children[0].value,this.regionOptions[0].children[0].children[0].value);
      
      this.fetchDevList();
    },
    methods:{
      fetchDevList(){
        let vm = this;
        let params = {
          kw: vm.kw,
          dev_room_id: vm.devRoomSelected,
          dev_type: vm.devTypeSelected,
          page_num: vm.page_num,
          page_size: vm.page_size
        }

        vm.loading = true;
        getDeviceList(vm.areaIds, params)
        .then(response => {
          let res = response.data;

          vm.list = res.rows;
          vm.page_num = res.page_num;
          vm.page_size = res.page_size;
          vm.total = res.total;

          vm.loading = false;
        })
        .catch(error => {
          vm.loading = false;
        })
      },
      fetchDevRoomList(){
        let vm = this;
        let params = {
          ril: 'S',
          page_num: 1,
          page_size: 1000
        };
        vm.devRoomListLoading = true;
        getDevRoomList(vm.areaIds, params)
        .then(response => {
          let res = response.data;
          vm.devRoomList = res.rows;
          //console.log(res.rows)
          vm.devRoomListLoading = false;
        })
        .catch(error => {

          vm.devRoomListLoading = false;
        })
      },
      handleDevRoomChange(val) {
        if (!val) {
          return;
        }
        this.fetchDevRoomList();
      },
      onDetailed:function(d_id){
        this.$router.push({
          path:"/sbtzxx",
          query:{
            id:d_id
          }
        })          
      },
      handleSizeChange(val) {//每页条数控制
        this.page_size = val;
        this.fetchDevList();
      },
      handleCurrentChange(val) {//每页
        this.page_num = val;
        this.fetchDevList();
      },
      onAdd:function(){
        this.$router.push({
          path:"/sbtzxz"
        })
      },
      onSearch(){
        this.fetchDevList();
      }
    }

  }

</script>  



